<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in roles" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles } from '@/api/roles'
import { getUserStaffPhoto } from '@/api/user'
import { assignRoles } from '@/api/employees '
export default {
  props: {
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [],
      roles: []
    }
  },
  created() {
    this.loadRoles()
    this.loadRoleIds()
  },
  methods: {
    async loadRoles() {
      try {
        const res = await getRoles()
        // console.log(res)
        this.roles = res.data.rows
      } catch (e) {
        console.log(e)
      }
    },

    closeDialog() {
      this.$emit('close')
    },
    async loadRoleIds() {
      try {
        const res = await getUserStaffPhoto(this.id)
        // console.log(res)
        this.roleIds = res.data.roleIds
      } catch (e) {
        console.log(e)
      }
    },
    resetRoles() {
      this.roleIds = []
    },
    async hSubmit() {
      try {
        const res = await assignRoles({
          id: this.id,
          roleIds: this.roleIds
        })
        console.log(res)
        this.$message.success(res.message)
        this.$emit('close')
      } catch (e) {
        this.$message.error(e.message)
      }
    }
  }
}
</script>
